package com.quiz.bis.controller;

import com.quiz.bis.domain.bo.AppealHandleBo;
import com.quiz.bis.domain.bo.ManualCorrectBo;
import com.quiz.bis.domain.bo.ScoreQueryBo;
import com.quiz.bis.domain.model.ExamQuestionAnswers;
import com.quiz.bis.domain.vo.*;
import com.quiz.bis.service.IScoreService;
import com.quiz.common.core.domain.R;
import com.quiz.common.mybatis.core.page.PageQuery;
import com.quiz.common.mybatis.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 成绩管理控制器
 */
@RestController
@RequestMapping("/score")
@Tag(name = "成绩管理")
@RequiredArgsConstructor
public class ScoreTeacherController {
    private final IScoreService scoreService;

    /**
     * 分页查询考试组中的成绩列表
     */
    @GetMapping("/list")
    public TableDataInfo<ExamScoreSummaryVo> list(@Validated ScoreQueryBo query, PageQuery pageQuery) {
        return scoreService.pageExamScoreList(query, pageQuery);
    }

    /**
     * 获取考试统计概览
     */
    @GetMapping("/summary/{examId}")
    public R<ExamScoreSummaryVo> getExamSummary(@PathVariable Long examId) {
        return R.ok(scoreService.getExamSummary(examId));
    }

    /**
     * 获取题目统计列表
     */
    @GetMapping("/question/stats/{examId}")
    public R<List<QuestionStatVo>> getQuestionStats(@PathVariable Long examId) {
        return R.ok(scoreService.getQuestionStats(examId));
    }

    /**
     * 分页查询考生成绩列表
     */
    @GetMapping("/teacher/list/{examId}")
    public TableDataInfo<StudentScoreVo> pageStudentScores(
            @PathVariable Long examId,
            PageQuery pageQuery,
            @RequestParam(required = false) String searchKey) {
        return scoreService.pageStudentScores(examId, pageQuery, searchKey);
    }

    /**
     * 获取学生考试详情
     */
    @GetMapping("/teacher/detail/{recordId}")
    public R<StudentExamScoreDetailVo> getStudentExamDetail(@PathVariable Long recordId) {
        return R.ok(scoreService.getStudentExamDetail(recordId));
    }

    /**
     * 人工批改题目
     */
    @PostMapping("/manual/correct")
    public R<Void> manualCorrect(@RequestBody ManualCorrectBo correctBo) {
        return scoreService.manualCorrect(correctBo);
    }

    /**
     * 获取待批改题目列表
     */
    @GetMapping("/manual/todo/{examId}")
    public R<List<ManualCorrectQuestionVo>> getManualCorrectQuestions(@PathVariable Long examId) {
        return R.ok(scoreService.getManualCorrectQuestions(examId));
    }

    /**
     * 获取待批改学生列表
     */
    @GetMapping("/manual/students/{examId}")
    public R<List<ManualCorrectStudentVo>> getManualCorrectStudents(@PathVariable Long examId) {
        return R.ok(scoreService.getManualCorrectStudents(examId));
    }

    /**
     * 发布成绩
     */
    @PostMapping("/publish/{examId}")
    public R<Void> publishScore(@PathVariable Long examId) {
        return scoreService.publishScore(examId);
    }

    /**
     * 获取题目详情（包括选项和答案）
     */
    @GetMapping("/question/detail/{questionId}")
    public R<QuestionDetailVo> getQuestionDetail(@PathVariable Long questionId) {
        return R.ok(scoreService.getQuestionDetail(questionId));
    }

    /**
     * 获取所有学生答案（支持按题目或按学生两种模式）
     */
    @GetMapping("/teacher/answer")
    public R<StudentAnswerVo> getStudentAnswer(
            @RequestParam(required = false) Long questionId,
            @RequestParam(required = false) Long recordId,
            @RequestParam(required = false) Long examId) {
        return R.ok(scoreService.getStudentAnswer(questionId, recordId, examId));
    }

    /**
     * 获取学生特定题答案（支持按题目或按学生两种模式）
     */
    @GetMapping("/teacher/answer/record")
    public R<ExamQuestionAnswers> getStudentAnswer(Long questionAnswerId) {
        return R.ok(scoreService.getStudentAnswerRecord(questionAnswerId));
    }

    /**
     * 处理申诉
     */
    @PostMapping("/appeal/handle")
    public R<Void> handleAppeal(@RequestBody AppealHandleBo handleBo) {
        return scoreService.handleAppeal(handleBo);
    }

    /**
     * 获取申诉列表
     */
    @GetMapping("/appeal/list/{examId}")
    public R<List<AppealVo>> getAppealList(@PathVariable Long examId) {
        return R.ok(scoreService.getAppealList(examId));
    }

    /**
     * 获取学生考试统计信息
     */
    @GetMapping("/teacher/statistics/{recordId}")
    public R<ExamStatisticsPerStuVo> getStudentExamStatistics(@PathVariable Long recordId) {
        return R.ok(scoreService.getStudentExamStatistics(recordId));
    }


}